$(function () {
    var a = false, b = false, c = false, d = false, e = false;

    $("#username").blur(function () {

        var username = $("#username").val();
        if (username == "") {
            $("#username").css("border", "1px solid red");
            $("#tips1").html("请输入用户名").css("color", "red");
            $(".tipss1").show();
            a = false;
        } else {

            $.ajax({
                type: "post",
                url: "/register/checkName",
                data: {
                    check_username: username
                },
                success: function (result) {
                    if (result == "该用户已被注册!") {
                        $("#username").val("").css("border", "1px solid red");
                        $("#tips1").html("该用户已被注册!").css("color", "red");
                        $(".tipss1").show();
                        a = false;
                    } else {
                        $("#username").css("border", "1px solid green");
                        $("#tips1").html("该用户可注册!").css("color", "green");
                        $(".tipss1").show().delay(1000).fadeOut();
                        a = true;
                    }

                },
                error: function (result) {
                    alert(result);
                    return;
                }
            })

        }
    })
    $("#password").blur(function () {
        var password = $("#password").val();
        if (password == "") {
            $("#password").css("border", "1px solid red");
            $("#tips2").html("请输入密码!").css("color", "red");
            $(".tipss2").show();
            b = false;
        } else {
            $("#password").css("border", "1px solid green");
            $("#tips2").html("密码合法!").css("color", "green");
            $(".tipss2").show().delay(1000).fadeOut();
            b = true;
        }

    })

    $("#password2").blur(function () {
        var password = $("#password2").val();
        var password2 = $("#password2").val();
        if (password2 == "") {
            $("#password2").css("border", "1px solid red");
            $("#tips3").html("请输入确认密码!").css("color", "red");
            $(".tipss3").show();
            c = false;
        } else if (password != password2) {
            $("#password").val("");
            $("#password2").val("").css("border", "1px solid red");
            $("#tips3").html("两次输入的密码不相同,请重新输入!").css("color", "red");
            $(".tipss3").show();
            c = false;
        } else {
            $("#password2").css("border", "1px solid green");
            $(".tipss3").hide();
            c = true;
        }

    })
    $("#email").blur(function () {

        var email = $("#email").val();
        if (email == "") {
            $("#email").css("border", "1px solid red");
            $("#tips4").html("请输入邮箱!").css("color", "red");
            $(".tipss4").show();
            d = false;
        } else {
            $("#email").css("border", "1px solid green");
            $("#tips4").html("邮箱合法!").css("color", "green");
            $(".tipss4").show().delay(1000).fadeOut();
            d = true;
        }

    })
    $("#phone").blur(function () {

        var phone = $("#phone").val();
        if (phone == "") {
            $("#phone").css("border", "1px solid red");
            $("#tips5").html("请输入手机号!").css("color", "red");
            $(".tipss5").show();
            e = false;
        } else {
            $("#phone").css("border", "1px solid green");
            $("#tips5").html("手机号合法!").css("color", "green");
            $(".tipss5").show().delay(1000).fadeOut();
            e = true;
        }

    })

    $(".submit").click(function () {

        if (a && b && c && d && e) {

            var username = $("#username").val();
            var password = $("#password").val();
            var email = $("#email").val();
            var phone = $("#phone").val();
            var account_type = $("input[name='type']:checked").val();
            $.ajax({
                type: "post",
                url: "/register/userRegister",
                dataType: "text",
                data: {username: username, email: email, phone: phone, password: password, account_type: account_type},
                success: function (result) {
                    if (result == "success") {
                        alert("注册成功!");
                        window.location.href = "/login";
                    } else {
                        alert("注册失败!");
                    }
                },
                error: function (result) {
                    alert(result);
                }
            })
        }
    })
})